'use strict';

const path = require('path')

const pkgDir = require('pkg-dir').sync;
const { isObject } = require('@cjx-cli-dev/utils')

class Package {
    // TODO
  constructor(options) {
    console.log('Package', options)
    
    if (!options) {
      throw Error('Package类的options的参数必须有值')
    }
    if (!isObject(options)) {
      throw Error('Package类的options的参数必须是个对象')
    }
    // package的目标路径
    this.targetPath = options.targetPath
    // package的 缓存路径
    // this.storePath = options.storePath
    // package的 name
    this.packageName = options.packageName
    // package的 version
    this.packageVersion = options.packageVersion
  }

  // 判断当前Package 是否存在
  exists() {

  }

  // 安装Package
  install() {

  }

  // 更新Package
  update() {

  }

  // 获取入口文件
  getRootFilePath() {
    // 1. 获取package.json所在目录 - pkg-dir
    // 2. 读取pack.json - require()
    // 3. main/lib -path
    // 4. 路径的兼容（macOS/windows)
    const dir = pkgDir(this.targetPath)
    console.log(2222, dir, this.targetPath)
    if (dir) {
      const testPath = '/Users/xiejie/Desktop/code/脚手架/cjx-cli-dev/models/package'
      const pagFile = require('/Users/xiejie/Desktop/code/脚手架/cjx-cli-dev/models/package' + '/package.json')
      
      console.log(444, pagFile)
      
    }
    
    return dir
    
    return null
  }
}

module.exports = Package;
